package com.cdvtc.flower.dao.impl;

import com.cdvtc.flower.dao.CustomerDao;
import com.cdvtc.flower.model.Customer;
import com.cdvtc.flower.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CustomerDaoImpl implements CustomerDao {
    private Connection conn;
    @Override
    public List<Customer> getAllCustomer() {
        List<Customer> customers = new ArrayList<>();

        // 获取连接
        conn = DBUtil.getConnection();
//        Statement st = null;


        // 创建Staement
        try(Statement st = conn.createStatement()) {
            // 执行SQL，获取ResultSet
            String sql = "select * from customer";
            try(ResultSet rs = st.executeQuery(sql)) {
                // 从rs中获取数据
                while (rs.next()) {
                    int customer_id = rs.getInt("customer_id");
                    String user_name = rs.getString("user_name");
                    String address = rs.getString("address");
                    String phone = rs.getString("phone");
                    String email = rs.getString("email");
                    String password = rs.getString("password");

                    // 将数据写入封装对象
                    Customer customer = new Customer();

                    customer.setCustomer_id(customer_id);
                    customer.setEmail(email);
                    customer.setPassword(password);
                    customer.setPhone(phone);
                    customer.setUser_name(user_name);
                    customer.setAddress(address);

                    //将对象往往集合类中
                    customers.add(customer);
                }
            }

            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return customers;
    }

    @Override
    public boolean addCustomer(Customer customer) {
        try {
            //获取连接
            Connection con = DBUtil.getConnection();

            //执行查询并返回结果集
            PreparedStatement pst = con.prepareStatement("insert into customer( user_name, phone, email, password, address) values(?, ?, ?, ?, ?)");
            pst.setString(1, customer.getUser_name());
            pst.setString(2, customer.getPhone());
            pst.setString(3, customer.getEmail());
            pst.setString(4, customer.getPassword());
            pst.setString(5, customer.getAddress());
            int result = pst.executeUpdate();

            //释放连接资源
            pst.close();
            con.close();

            return result > 0; //大于0表示成功创建一行
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    @Override
    public boolean isUserExisted(String key, String value) {
        try {
            //获取连接
            Connection conn = DBUtil.getConnection();

            //执行查询并返回结果集
            PreparedStatement pst = conn.prepareStatement("select * from customer where " + key + "=?");
            pst.setString(1, value);

            ResultSet rs = pst.executeQuery();

            boolean result = rs.next(); //判断是否查询到数据

            //释放连接资源
            rs.close();
            pst.close();
            conn.close();

            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }
    @Override
    public Customer userLogin(int customer_id, String password) {
        Customer customer = null;
        conn = DBUtil.getConnection();
        try {
            String query = "select * from customer where customer_id=? and password=?";
            PreparedStatement pst = this.conn.prepareStatement(query);
            pst.setInt(1, customer_id);
            pst.setString(2, password);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                customer = new Customer();
                customer.setCustomer_id(rs.getInt("customer_id"));
                customer.setUser_name(rs.getString("user_name"));
                customer.setAddress(rs.getString("address"));
                customer.setPhone(rs.getString("phone"));
                customer.setEmail(rs.getString("email"));
                customer.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            System.out.print(e.getMessage());
        }
        return customer;
    }

    public static void main(String[] args) {
        CustomerDao customerDao = new CustomerDaoImpl();
        System.out.println(customerDao.getAllCustomer());
    }
}
